// packageI/knowledge/knowledgeIndex/knowledgeIndex.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: {},
    images: [],
    new_list: [],
    hot_list: [],
    list: {
      data: [],
      title: '推荐知识库'
    },
    keyword: "",
    plugin: 'message-base', //message-base商城知识库，project-template供应链知识库
    apiOption: {},
    itemOption: {
      article_name: 'article_name',
      creater_name: 'creater_name',
      base_name: 'base_name',
      created_at: 'created_at'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData();
    this.getdata();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  initData() {
    let api = new Map([
      ["message-base", {
        index: 'plugin.message-base.frontend.base.firstPage',
        callback: (res) => {
          let {
            new_list,
            hot_list,
            list,
            slide,
            keyword
          } = res.data;
          this.setData({
            new_list,
            hot_list,
            keyword,
            images: slide,
            ['list.data']: list.data,
            ['list.title']: '推荐知识库'
          });
        }
      }],
      ["project-template", {
        index: 'plugin.project-template.frontend.knowledge.index',
        callback: (res) => {
          let {
            knowledge_base,
            knowledge_base_article
          } = res.data;
          this.setData({
            new_list: knowledge_base_article,
            ['list.data']: knowledge_base,
            ['list.title']: '知识库',
            itemOption: {
              article_name: 'title',
              base_name: 'has_one_yz_supply_knowledge_base.title',
              created_at: 'release_time_at'
            }
          });
        }
      }],
    ]);
    if (this.options.plu) {
      this.data.plugin = this.options.plu;
    }
    this.setData({
      plugin: this.data.plugin,
      apiOption:  api.get(this.data.plugin),
    });
  },

  getdata() {
    let urlStr = app.getNetAddresss(this.data.apiOption.index);
    app._getNetWork({
      url: urlStr,
      data: {show_type: 2},
      success: (res) => {
        if (res.data.result !== 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
          return;
        }
        this.data.apiOption.callback(res.data);
      }
    });
  },
  toUrl(e) {
    let _data = e.currentTarget.dataset || {};
    this.wxRouterLink(_data);
  },
  wxRouterLink(_data) {
    let _Param = '';
    const {
      url,
      ...otherParam
    } = _data;
    Object.keys(otherParam).forEach(function (key) {
      if (otherParam[key] !== '') _Param += key + '=' + otherParam[key] + '&'
    });
    if (url === '') return;
    wx.navigateTo({
      url: url + '?' + _Param.slice(0, -1)
    })
  },
  onListClick(e){
    let {id,base_id,knowledge_base_id} = e.detail;
    this.wxRouterLink({url: '/packageI/knowledge/knowledgeDetail/knowledgeDetail',id: id,bid: base_id||knowledge_base_id,plu: this.data.plugin});
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let value = wx.getStorageSync("yz_uid");
    let mid = "";
    if (value) {
      mid = value;
    }
    return {
      path: '/packageI/knowledge/knowledgeIndex/knowledgeIndex?plu=' + this.options.plu + '&mid=' + mid
    }
  }
})